package com.example.chatgpt;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.Application;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.example.chatgpt.adapter.MessageInfoAdapter;
import com.example.chatgpt.bean.User;
import com.example.chatgpt.dao.MessageDao;
import com.example.chatgpt.entity.MessageInfo;
import com.example.chatgpt.util.StatusBarUtil;
import com.example.chatgpt.util.TimeUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ChatHistoryActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private Calendar calendar;
    private int year;
    private int month;
    private int day;
    private TextView tv_date;
    private String format;
    private MessageDao messageDao;
    private RecyclerView rv_message;
    private List<MessageInfo> list;
    private MessageInfoAdapter adapter;
    private User userInfo;
    private MyApplication mApplication;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_history);
        //设置状态栏字体为黑色
        StatusBarUtil.setStatusBarTextColorBlack(this);

        //获取登陆界面的user
        mApplication = (MyApplication) getApplication();
        //获取数据库
        messageDao = mApplication.getMessageDao();
        userInfo = mApplication.getUserInfo();

        tv_date = findViewById(R.id.tv_date);
        tv_date.setText(TimeUtil.getDateNow());
        format = TimeUtil.getDateNow();
        tv_date.setOnClickListener(this);
        rv_message = findViewById(R.id.rv_message);
        findViewById(R.id.ib_query).setOnClickListener(this);
        findViewById(R.id.ib_back_chat).setOnClickListener(this);
        findViewById(R.id.ib_delete).setOnClickListener(this);

        //设置日期
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);

        //设置recycleView
        list = new ArrayList<>();
        adapter = new MessageInfoAdapter(list);
        rv_message.setLayoutManager(new LinearLayoutManager(this));
        rv_message.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //设置时间
            case R.id.tv_date:
                DatePickerDialog datePickerDialog = new DatePickerDialog((Context) this, this, year, month, day);
                datePickerDialog.show();
                break;
            //按时间查询
            case R.id.ib_query:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list = messageDao.queryByDate(format, userInfo.getUser());
                        adapter.setList(list);
                        adapter.notifyDataSetChanged();
                        rv_message.smoothScrollToPosition(adapter.getItemCount());
                    }
                });
                break;
            //返回聊天页面
            case R.id.ib_back_chat:
                finish();
                break;
            //删除这一天的数据库信息
            case R.id.ib_delete:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        messageDao.deleteByDate(format, userInfo.getUser());
                        list = messageDao.queryByDate(format, userInfo.getUser());
                        adapter.setList(list);
                        adapter.notifyDataSetChanged();
                        rv_message.smoothScrollToPosition(adapter.getItemCount());
                    }
                });
        }
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        String strMonth = (month + 1) < 10 ? "0" + (month + 1) : String.valueOf(month + 1);
        String strDay = day < 10 ? "0" + day : String.valueOf(day);
        String strYear = String.valueOf(year);
        format = strYear + ":" + strMonth + ":" + strDay;
        Log.d("guaimiao", format);
        tv_date.setText(format);
    }

}